package com.mall.util;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * @author huyongtao
 * @date 2022/5/31
 */
public class QRUtil {

    //生成二维码图片
    public static void code() {
        String content = "https://www.baidu.com";
        String path = "D:/upload/zip";// 二维码保存的路径
        String ext = "jpg";
        File imgDirFile = new File(path);
        if (!imgDirFile.exists()) {
            imgDirFile.mkdirs();
        }
        QrConfig config = QrConfig.create().setImg("D:\\upload\\1.jpg").setHeight(400).setWidth(400);
        for(int i=0;i<10;i++){
            String codeName = UUID.randomUUID().toString()+"."+ext;// 二维码的图片名
            String imgPath = path+File.separator+codeName;
            File saveFile = new File(imgPath);
            BufferedImage image = QrCodeUtil.generate(content, config);
            try {
                ImageIO.write(image,ext,saveFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        addZip(path);
    }

    //生成压缩包
    public static void addZip(String templateDir){
        //会直接生成一个压缩包
        ZipUtil.zip(templateDir);
        delAllFile(templateDir);
    }

    //生成二维码图片
    public static void downQRCode(HttpServletResponse response) {
        String content = "https://www.baidu.com";
        String path = "D:/upload/zip";// 二维码保存的路径
        String ext = "jpg";
        File imgDirFile = new File(path);
        if (!imgDirFile.exists()) {
            imgDirFile.mkdirs();
        }
        QrConfig config = QrConfig.create().setImg("D:\\upload\\1.jpg").setHeight(400).setWidth(400);
        for(int i=0;i<10;i++){
            String codeName = UUID.randomUUID().toString()+"."+ext;// 二维码的图片名
            String imgPath = path+File.separator+codeName;
            File saveFile = new File(imgPath);
            BufferedImage image = QrCodeUtil.generate(content, config);
            try {
                ImageIO.write(image,ext,saveFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        downZip(path,response);
    }


    public static void downZip(String templateDir,HttpServletResponse response){
        File file = ZipUtil.zip(templateDir);
        InputStream fis = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            String fileName = URLEncoder.encode("压缩包", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".zip");
            response.setContentType("application/x-download;charset=utf-8");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(fis!=null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        delAllFile(templateDir);
    }


    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                flag = true;
            }
        }
        file.delete();
        return flag;
    }
}
